package zjh_20230331;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Demo {
    public static void main(String[] args) {
        SAXReader saxReader = new SAXReader();
        File file = new File("D:\\kaifamiao\\code\\untitled2\\src\\zjh_20230331\\translate.xml");
        try {

            Document read = saxReader.read(file);
            Element rootElement = read.getRootElement();

            Element element = rootElement.addElement("Student");

            Element name = element.addElement("name");
            name.setText("ww");
            Element id = element.addElement("id");
            id.setData(2);
            Element gender = element.addElement("gender");
            gender.setText("男");
            element.addAttribute("id","3");

            XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("D:\\kaifamiao\\code\\untitled2\\src\\zjh_20230331\\translate-new.xml"));
            xmlWriter.write(read);


        } catch (DocumentException e) {
            throw new RuntimeException(e);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
